﻿using System;
using ThoughtWorks.CruiseControl.Core.Tasks.Conditions;
using NUnit.Framework;
using Rhino.Mocks;
using ThoughtWorks.CruiseControl.Core;
using ThoughtWorks.CruiseControl.Remote;

namespace ThoughtWorks.CruiseControl.UnitTests.Core.Tasks.Conditions
{
    [TestFixture]
    public class statusTaskConditionTest
    {
        private void executeTest(IntegrationStatus status, IntegrationStatus testStatus, bool expectedResult)
        {
            MockRepository mocks = new MockRepository();
            IIntegrationResult integrationResult = mocks.StrictMock<IIntegrationResult>();
            Expect.Call(integrationResult.Status).Return(status).Repeat.Any();
            mocks.ReplayAll();

            var condition = new StatusTaskCondition();
            condition.Status = testStatus;

            string message = string.Format("comparing : '{0}' to '{1}' and expected '{2}'", status, testStatus, expectedResult);
            Assert.That(condition.Eval(integrationResult), Is.EqualTo(expectedResult), message);

            mocks.VerifyAll();
        }

        [Test]
        public void BulkTest()
        {
            var statuses = new[] {IntegrationStatus.Cancelled, IntegrationStatus.Exception, IntegrationStatus.Failure, IntegrationStatus.Success, IntegrationStatus.Unknown};
            var compareStatuses = new[] { IntegrationStatus.Cancelled, IntegrationStatus.Exception, IntegrationStatus.Failure, IntegrationStatus.Success, IntegrationStatus.Unknown };

            foreach (IntegrationStatus integrationStatus in statuses)
                foreach (var compareStatus in compareStatuses)
                    executeTest(integrationStatus, compareStatus, (string.Equals(integrationStatus.ToString(), compareStatus.ToString(), StringComparison.InvariantCultureIgnoreCase)));
        }

        [Test]
        [ExpectedException(typeof (ArgumentException))]
        public void nullResultThrowsArgumentException()
        {
            var condition = new StatusTaskCondition();
            condition.Status = IntegrationStatus.Success;
            condition.Eval(null);
        }

        //[Test]
        //[ExpectedException(typeof (ArgumentException))]
        //public void nullStatusThrowsArgumentException()
        //{
        //    MockRepository mocks = new MockRepository();
        //    IIntegrationResult integrationResult = mocks.StrictMock<IIntegrationResult>();
        //    Expect.Call(integrationResult.Status).Return(IntegrationStatus.Success).Repeat.Any();
        //    mocks.ReplayAll();

        //    var condition = new StatusTaskCondition();
        //    condition.Status = null;
        //    condition.Eval(integrationResult);

        //    mocks.VerifyAll();
        //}
    }
}